﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using Eniux.Windows.ThreeDEngine.Helper;
using System.Windows;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    ///描述一个箭头3D模型
    /// </summary>
    public class ArrowVisual3D : MeshElement3D
    {
        #region 注册依赖属性

        #region Diameter（直径）
        /// <summary>
        /// 获取或设置直径
        /// </summary>
        public double Diameter
        {
            get { return (double)GetValue(DiameterProperty); }
            set { SetValue(DiameterProperty, value); }
        }
        /// <summary>
        /// 直径属性
        /// </summary>
        public static readonly DependencyProperty DiameterProperty = DependencyProperty.Register(
            "Diameter", typeof(double), typeof(ArrowVisual3D), new UIPropertyMetadata(1.0, GeometryChanged)); 
        #endregion

        #region HeadLength（头部长度）
        /// <summary>
        /// 获取或设置头部长度
        /// </summary>
        public double HeadLength
        {
            get { return (double)GetValue(HeadLengthProperty); }
            set { SetValue(HeadLengthProperty, value); }
        }
        /// <summary>
        /// 头部长度属性
        /// </summary>
        public static readonly DependencyProperty HeadLengthProperty = DependencyProperty.Register(
            "HeadLength", typeof(double), typeof(ArrowVisual3D), new UIPropertyMetadata(3.0, GeometryChanged)); 
        #endregion

        #region Point1(第一个点)
        /// <summary>
        /// 获取或设置第一个点
        /// </summary>
        public Point3D Point1
        {
            get { return (Point3D)GetValue(Point1Property); }
            set { SetValue(Point1Property, value); }
        }

        /// <summary>
        /// 第一个点属性
        /// </summary>
        public static readonly DependencyProperty Point1Property = DependencyProperty.Register(
            "Point1",
            typeof(Point3D),
            typeof(ArrowVisual3D),
            new UIPropertyMetadata(new Point3D(0, 0, 0), GeometryChanged)); 
        #endregion

        #region Point2（第二个点）
        /// <summary>
        /// 获取或设置第二个点
        /// </summary>
        public Point3D Point2
        {
            get { return (Point3D)GetValue(Point2Property); }
            set
            {
                SetValue(Point2Property, value);
            }
        }
        /// <summary>
        /// 第二个点属性
        /// </summary>
        public static readonly DependencyProperty Point2Property = DependencyProperty.Register(
            "Point2",
            typeof(Point3D),
            typeof(ArrowVisual3D),
            new UIPropertyMetadata(new Point3D(0, 0, 10), GeometryChanged)); 
        #endregion

        #region ThetaDiv
        /// <summary>
        /// 
        /// </summary>
        public int ThetaDiv
        {
            get { return (int)GetValue(ThetaDivProperty); }
            set { SetValue(ThetaDivProperty, value); }
        }
        /// <summary>
        ///
        /// </summary>
        public static readonly DependencyProperty ThetaDivProperty = DependencyProperty.Register(
            "ThetaDiv", typeof(int), typeof(ArrowVisual3D), new UIPropertyMetadata(36, GeometryChanged)); 
        #endregion

        #endregion

        #region 公共属性

        /// <summary>
        /// 获取或设置方向
        /// </summary>
        public Vector3D Direction
        {
            get{return Point2 - Point1;}
            set{Point2 = Point1 + value;}
        }

        /// <summary>
        /// 获取或设置源点
        /// </summary>
        public Point3D Origin
        {
            get{return Point1;}
            set{ Point1 = value;}
        }

        #endregion

        #region 方法

        /// <summary>
        /// 生成几何模型
        /// </summary>
        /// <returns></returns>
        protected override MeshGeometry3D Tessellate()
        {
            var builder = new MeshBuilder(true, true);
            builder.AddArrow(Point1, Point2, Diameter, HeadLength, ThetaDiv);
            return builder.ToMesh();
        }

        #endregion
    }
}
